- Write a complete C++ program that does the following:
- Prompt the user for a positive integer greater than or equal to 10.
- If the user enters an invalid input, force the user to enter a valid input.
- Print to the monitor a random number between 1 and the user's number.
Sample run of program:
Enter a number greater than or equal to 10: 7
Invalid input! Try again: 11
Random number between 1 and 11 is 9
- Write a complete C++ program that does the following:
- Prompt the user for two positive integers. The difference in value between the two integers must be at least 10. For example:
- 9 and 15 would be invalid inputs because 15 is only 6 more than 9.
- 14 and 25 would be valid inputs because the difference between the two is 11, which is greater than 10.
- If the user enters an invalid input, tell the user "Invalid input" and force the user to enter valid inputs.
- Print to the monitor a random number between the smaller number entered by the user and the larger number entered by the user.
Sample run of program:
Enter two positive integers with a difference of at least 10: -1 15
Invalid input! Try again: 5 9
Invalid input! Try again: 15 39
Random number between 15 and 39 is 23
- Write a complete C++ program that simulates rolls of a die.
- The program will prompt the user for a positive integer, which will be the number of die rolls simulated.
- If the user enters an invalid number, tell the user we are not playing games and end the program.
- If the user enters a valid number n, use a loop to simulate to simulate n die rolls.
- For each die roll, print the roll number and the simulated die roll value.
- At the end of the die rolls, the program will print the total count of ones rolled.
Sample run of program:
Enter a number of die rolls: 5
Roll 1: 2
Roll 2: 3
Roll 3: 6
Roll 4: 1
Roll 5: 2
Total number of ones rolled: 1
- Write a C++ program to play a guessing game:
- The computer randomly generates a number between 1 and 100 inclusive and asks the user to guess the number.
- After each guess, the computer gives a hint:
- If the guess is bigger the program prints "too big".
- If the guess is smaller the program prints "too small".
- The computer then asks the user to guess again.
- The computer keeps track of the number of guesses.
- When the user's guess is correct, it prints "Congratulations! You took n guesses" where n is the number of guesses.
Sample run of program:
Guess the number I'm thinking of between 1 and 100: 50
Too big
40
Too small
45
Too big
43
Too small
44
Congratulations! You took 5 guesses.
- Write a C++ program to simulate repeated coin tosses with a penny for 10, 100, 1000, and 5000 repeats.
- Simulate each toss by randomly generating a number between 0 and 1 inclusive.
- 0 is tails and 1 is heads.
- Keep a count of the numbers of 0's and 1's generated per set of simulations.
- Divide the resulting counts by the total number of simulations to compute the relative frequency of each possible result.
- See how the probability of getting heads versus probability of getting tails changes with the number of simulations.
- Do these probabilities stabilize as the number of simulations increase?
Sample run of program:
Probability of heads given 10 tosses = 0.6
Probability of tails given 10 tosses = 0.4
Probability of heads given 100 tosses = 0.53
Probability of tails given 100 tosses = 0.47
Probability of heads given 1000 tosses = 0.484
Probability of tails given 1000 tosses = 0.516
Probability of heads given 5000 tosses = 0.4974
Probability of tails given 5000 tosses = 0.5026